/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.faimg.pomar.modelo.dao;

import br.faimg.pomar.modelo.interfaces.CRUDInterface;
import br.faimg.pomar.modelo.pojo.GrupodeUsuario;
import br.faimg.pomar.modelo.pojo.Usuario;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UsuarioDAO implements CRUDInterface<Usuario> {
    
    public void create(Usuario pojo) {
         Connection con = null;
         PreparedStatement stm = null;
         String sql = "insert into USUARIO values(?,?,?,?,?);";
        /*
  usu_login character(6) NOT NULL,
  usu_grp_codigo character(6) NOT NULL,
  usu_senha character(6) NOT NULL,
  usu_nome character varying(30) DEFAULT NULL::character varying,
  usu_email character(20) DEFAULT NULL::bpchar,
  CONSTRAINT usuario_pkey PRIMARY KEY (usu_login),
  CONSTRAINT usuario_usu_grp_codigo_fkey FOREIGN KEY (usu_grp_codigo)
      REFERENCES grupo_usuario (grp_codigo) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
         */
         
         
         try {

             con = ConnectionManager.getInstance().getConnection();
             stm = con.prepareStatement(sql);
             stm.setString(4, pojo.getNome());
             stm.setString(5, pojo.getEmail());
             stm.setString(1, pojo.getLogin());
             stm.setString(3, pojo.getSenha());
             stm.setString(2, pojo.getGrupo().getCodigo());
             stm.execute();
        
         } catch (SQLException ex) {
            Logger.getLogger(Usuario.class.getName()).log(Level.SEVERE, null, ex);
        }finally {
            
            if (stm != null){
             
                try {
                    stm.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Usuario.class.getName()).log(Level.SEVERE, null, ex);
                }
            
            }
            
        
            if (con != null){
                
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Usuario.class.getName()).log(Level.SEVERE, null, ex);
                }
            
            }
        
        }
    }

 public void update(Usuario pojo) {
    Connection con = null;
    PreparedStatement stm = null;
    String sql = "update USUARIO set usu_senha = ?" + 
                 "usu_nome = ?" + "usu_email = ?" +
                 "where usu_login = ?;";

    try {

        con = ConnectionManager.getInstance().getConnection();
        stm = con.prepareStatement(sql);
        
        stm.setString(1, pojo.getSenha());
        stm.setString(2, pojo.getNome());
        stm.setString(3, pojo.getEmail());
        stm.setString(4, pojo.getLogin());
        
        stm.execute();

     } catch (SQLException ex) {
            Logger.getLogger(TipodeProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {

            if (stm != null) {

                try {
                    stm.close();
                } catch (SQLException ex) {
                    Logger.getLogger(TipodeProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
                }

            }


            if (con != null) {

                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(TipodeProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }
    }

    public List<Usuario> readByExample(Usuario pojo) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;

        String sql = "select * from usuario,grupo_usuario where usu_nome ilike ? and usu_grp_codigo = grp_codigo";
        
        List<Usuario> usuarios = new ArrayList<Usuario>();

        if (pojo == null) {

            pojo = new Usuario();
        }

        if ((pojo.getNome() == null) || 
            (pojo.getEmail() == null) || 
            (pojo.getLogin() == null)) {
            
            pojo.setNome("");
            pojo.setEmail("");
            pojo.setLogin("");
        }

        con = ConnectionManager.getInstance().getConnection();
        try {
            stm = con.prepareStatement(sql);
            stm.setString(1, "%"+ pojo.getNome() + "%");
    
            rs = stm.executeQuery();

            while (rs.next()) {
                
                Usuario temp = new Usuario();
                GrupodeUsuario gtemp = new GrupodeUsuario();
                
                temp.setLogin(rs.getString("usu_login")); 
                temp.setNome(rs.getString("usu_nome"));
                temp.setEmail(rs.getString("usu_email"));
                
                gtemp.setCodigo(rs.getString("grp_codigo"));
                gtemp.setDescricao(rs.getString("grp_descricao"));
                              
                temp.setGrupo(gtemp);
                
                usuarios.add(temp);
            }

        } catch (SQLException ex) {
            Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return usuarios;
    }

    public Usuario readById(String codigo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(String codigo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Usuario readById(Integer codigo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(Integer codigo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
